package com.sgb.kjwl.view.ui.activity.demand;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;

import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;

import com.flyco.tablayout.listener.OnTabSelectListener;
import com.sgb.alimaplibrary.SelectAddressActivity;
import com.sgb.goods.entity.GoodsItemEntity;
import com.sgb.goods.view.activity.inquiry.NewPurchasesActivity;
import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityDemandHallBinding;
import com.sgb.kjwl.di.home.DaggerHomeComponent;
import com.sgb.kjwl.di.home.HomeModule;
import com.sgb.kjwl.model.entity.supplier3.DictEntity3;
import com.sgb.kjwl.view.ui.fragment.demandHall.DemandHallListFragment;
import com.sgb.kjwl.view.widget.DemandHallTypePopWindow;
import com.sgb.kjwl.viewmodel.demand.DemandHallModel;
import com.sgb.link.event.LeaseEvent;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.event.EventConstant;
import com.swgk.core.recyclerview.adapter.FragPagerAdapter;
import com.swgk.core.util.MToast;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

/**
 * 首页商品：需求大厅界面
 * @author Administrator
 */
public class DemandHallActivity extends AppActivity {

    private ActivityDemandHallBinding binding;
    private ArrayList<Fragment> mFragments;

    @Inject
    DemandHallModel demandHall;
    private String[] titles;
    private FragPagerAdapter pagerAdapter;
    List<DictEntity3> dictdata;
    private DemandHallTypePopWindow demandHallTypePopWindow;
    private int state;

    public static void start(Context context) {
        Intent intent = new Intent(context, DemandHallActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerHomeComponent.builder()
                .appComponent(appComponent)
                .homeModule(new HomeModule(this))
                .build()
                .inject(this);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Override
    protected void initView() {
        binding = DataBindingUtil.setContentView(this, R.layout.activity_demand_hall);
        binding.backView.setOnClickListener(this);
        binding.demandpublish.setOnClickListener(this);
        binding.demandlocation.setOnClickListener(this);
        binding.editView.setOnClickListener(this);
//      商品类型
        demandHallTypePopWindow = new DemandHallTypePopWindow(this);
        demandHallTypePopWindow.setOnResultClick(new BasePopWindow.OnResultClick() {
            @Override
            public void result(Object key1, Object key2, Object key3) {
                binding.tablayout.setCurrentTab((int) key2);
            }
        });
        binding.tablayout.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                setTabLayoutName(position);
                demandHallTypePopWindow.setDefItem(position);
            }

            @Override
            public void onTabReselect(int position) {

            }
        });
        binding.searchShopPrice.setOnKeyListener((v, keyCode, event) -> {
            if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_DOWN) {
                closeKeybord();
                EventBus.getDefault().postSticky(LeaseEvent.make().setMessageTag(EventConstant.EVENT_SELECT_DEMANDHALL).setMessageData(binding.searchShopPrice.getText().toString()));
            }
            return false;
        });
    }

    //获取tab页签名称
    private void setTabLayoutName(int position) {
        if (null != dictdata && dictdata.size() > 0) {
            EventBus.getDefault().postSticky(LeaseEvent.make().setMessageTag(EventConstant.EVENT_SELECT_DEMANDHALL_TAB).setMessageData(dictdata.get(position).getName() + ""));
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back_view:
                finish();
                break;
            case R.id.demandpublish://需求大厅发布
                Intent createGood = new Intent(this, NewPurchasesActivity.class);
                createGood.putExtra("GOOD_EDIT", false);
                createGood.putExtra("GOOD_ADD", false);
                createGood.putExtra("GOOD_ENTITY", new GoodsItemEntity());
                startActivity(createGood);
                break;
            case R.id.demandlocation://需求大厅定位
                Intent intent = new Intent();
                intent.setClass(this, SelectAddressActivity.class);
                intent.putExtra(SelectAddressActivity.TITLE, "地址定位");
                intent.putExtra(SelectAddressActivity.HINT, "请输入你的定位地址");
                intent.putExtra(SelectAddressActivity.SAVE_TEXT, "使用该定位地址");
                intent.putExtra(SelectAddressActivity.CITY, "");
                startActivityForResult(intent, 999);
                break;
            case R.id.edit_view://材料类型下拉框
                if (null == dictdata || dictdata.size() == 0) {
                    return;
                }
                demandHallTypePopWindow.showPopwindow(binding.editView, 0, 0);
                demandHallTypePopWindow.setData(dictdata, binding.tablayout.getCurrentTab());
                break;
        }
    }


    @Override
    protected void init() {
//        获取tab材料类型
        demandHall.getDemandHallList();
    }

    /*
     * 根据接口返回的tab页签数来动态加载对应的fragment页面和viewpager绑定
     * */
    public void getDemanHallTabList(List<DictEntity3> data) {
        if (null != data && data.size() > 0) {
            dictdata = data;
            mFragments = new ArrayList<>();
            int size = data.size();
            titles = new String[size];
            for (int i = 0; i < size; i++) {
                mFragments.add(DemandHallListFragment.newInstance(data.get(i).getName()));
                titles[i] = data.get(i).getName();
            }
            pagerAdapter = new FragPagerAdapter(getSupportFragmentManager(), mFragments);
            binding.viewpager.setAdapter(pagerAdapter);
            binding.viewpager.setOffscreenPageLimit(titles.length - 1);
            binding.tablayout.setViewPager(binding.viewpager, titles);
        } else {
            MToast.showToast(getActivity(), "数据未加载");
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 0 && resultCode == 1) {
            state = data.getIntExtra("state", -1);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
